package cn.org.tpeach.nosql.view;

import cn.org.tpeach.nosql.tools.DateTools;
import cn.org.tpeach.nosql.tools.StringUtils;
import javafx.scene.control.OverrunStyle;
import javafx.scene.control.TableCell;
import javafx.scene.control.Tooltip;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class CenteredOverrunTableCell<S,T> extends TableCell<S, T> {
        private boolean showTime;
        public CenteredOverrunTableCell() {
            this(null,false);
        }
        public CenteredOverrunTableCell(String ellipsisString){
               this(ellipsisString,false);
        }
        public CenteredOverrunTableCell(String ellipsisString,boolean showTime) {
            super();
            this.showTime = showTime;
            setTextOverrun(OverrunStyle.CENTER_WORD_ELLIPSIS);
            if (ellipsisString != null) {
                setEllipsisString(ellipsisString);
            }  
        }

        @Override
        protected void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            String text = item == null ? "" : item.toString();
            if(StringUtils.isEmpty(text)){
                return;
            }
            try{
                if(StringUtils.isNotBlank(text) && showTime){
                    this.setTooltip(new Tooltip( DateTools.format(DateTools.asLocalDateTime(Long.parseLong(text)), DateTools.DATE_TIME)));
                }else{
                    this.setTooltip(new Tooltip(text));
                }
            }catch (Exception e){
                this.setTooltip(new Tooltip(text));
            }
            setText(text.replace("\n",""));
        }
    }